package com.iaz.tech.tools.uml.file.impl;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.iaz.tech.tools.uml.IUMLNeuronAble;
import com.iaz.tech.tools.uml.file.NeuronJSONTools;
import com.iaz.tech.tools.uml.relationship.impl.NeuronMessageRelationship;

import lombok.Data;
import lombok.EqualsAndHashCode;

@EqualsAndHashCode(callSuper=false)
@Data
public class NeuronMessageLoader extends NeuronJSONTools {
    /**
     * 日志
     */
	private final static Logger logger = (Logger) LoggerFactory.getLogger(NeuronMessageLoader.class);

    @Override
    public void parseJSON(IUMLNeuronAble umlObj, JSONObject json) {
        try {
            if (umlObj == null) {
                throw new IllegalArgumentException("umlObj 不能为空");
            }
            if (json == null) {
                throw new IllegalArgumentException("json 不能为空");
            }
            if (!(umlObj instanceof NeuronMessageRelationship)){
                throw new IllegalArgumentException("umlObj 必须是 NeuronExtendRelationship 类型");
            } 
            NeuronMessageRelationship messageRel = (NeuronMessageRelationship) umlObj;
            if (json.containsKey("dialing")) {
                messageRel.setDialingStr(json.getString("dialing"));
            }
            if (json.containsKey("called")) {
                messageRel.setCalledStr(json.getString("called"));
            }
            if (json.containsKey("messageName")) {
                messageRel.setMessage(json.getString("messageName"));
            }
            if (json.containsKey("dscr")) {
                messageRel.setDscr(json.getString("dscr"));
            }
            if (json.containsKey("hasReturn")) {
                messageRel.setHasReturn(json.getBoolean("hasReturn"));
            }

        } catch (Exception e) {
            logger.error("解析包含关系失败", e);
        }        
    }

    @Override
    public JSONObject toJSON(IUMLNeuronAble objStruct) {
        JSONObject result = new JSONObject();
        try {
            if (objStruct == null) {
                throw new IllegalArgumentException("objStruct 不能为空");
            }
            if (!(objStruct instanceof NeuronMessageRelationship)){
                throw new IllegalArgumentException("objStruct 必须是 NeuronExtendRelationship 类型");
            } 
            NeuronMessageRelationship messageRel = (NeuronMessageRelationship) objStruct;            
            result.put("dialing", messageRel.getDialingStr());
            result.put("called", messageRel.getCalledStr()); 
            result.put("messageName", messageRel.getMessage());
            result.put("dscr", messageRel.getDscr());
            result.put("hasReturn", messageRel.isHasReturn()); 
        }catch(Exception e) {
            logger.error("转换包含关系失败", e);
            result = new JSONObject();
        }
        return result;
    }

}
